package com.kamistoat.meimeimall.meimeimallcoupon.service.impl;

import com.kamistoat.meimeimall.common.To.productTo.SkuMemberPriceTo;
import com.kamistoat.meimeimall.common.To.productTo.SkuReductionTo;
import com.kamistoat.meimeimall.common.constant.DatabaseConstant;
import com.kamistoat.meimeimall.meimeimallcoupon.dao.MemberPriceDao;
import com.kamistoat.meimeimall.meimeimallcoupon.dao.SkuLadderDao;
import com.kamistoat.meimeimall.meimeimallcoupon.entity.MemberPriceEntity;
import com.kamistoat.meimeimall.meimeimallcoupon.entity.SkuLadderEntity;
import com.kamistoat.meimeimall.meimeimallcoupon.service.MemberPriceService;
import com.kamistoat.meimeimall.meimeimallcoupon.service.SkuLadderService;
import com.kamistoat.meimeimall.meimeimallcoupon.dao.SkuFullReductionDao;
import com.kamistoat.meimeimall.meimeimallcoupon.entity.SkuFullReductionEntity;
import com.kamistoat.meimeimall.meimeimallcoupon.service.SkuFullReductionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl implements SkuFullReductionService {

    @Autowired
    SkuFullReductionDao skuFullReductionDao;

    @Autowired
    SkuLadderDao skuLadderDao;
    @Autowired
    MemberPriceDao memberPriceDao;

    /**
     * 保存商品满减信息
     *
     * @param skuReductionTo 参数
     */
    @Override
    public void saveSkuReductionTo(SkuReductionTo skuReductionTo) {
        // 只保存有值的折扣/满减/会员价，没有的话直接不保存
        // 将打折信息保存到`cms_sku_ladder`中
        if (skuReductionTo.getFullCount() > 0) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderDao.insert(skuLadderEntity);
        }
        // 将SkuReductionTo中的满减信息保存到`cms_sku_full_reduction`中
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) > 0) {
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(1);
            skuFullReductionDao.insert(skuFullReductionEntity);
        }
        // 将会员价保存到`cms_member_price`中
        List<SkuMemberPriceTo> skuMemberPriceToList = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntityList = skuMemberPriceToList.stream().map(skuMemberPriceTo -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(skuMemberPriceTo.getId());
            memberPriceEntity.setMemberLevelName(skuMemberPriceTo.getName());
            memberPriceEntity.setMemberPrice(skuMemberPriceTo.getPrice());
            memberPriceEntity.setAddOther(1);
            memberPriceEntity.setCreateTime(new Date());
            memberPriceEntity.setUpdateTime(new Date());
            memberPriceEntity.setDelete(DatabaseConstant.UN_LOGISTIC_DELETED);
            return memberPriceEntity;
        }).filter(memberPriceEntity -> {
            // 用filter过滤掉没有设置会员价格的会员等级
            return memberPriceEntity.getMemberPrice().compareTo(new BigDecimal("0")) > 0;
        }).collect(Collectors.toList());
        memberPriceDao.insertBatch(memberPriceEntityList);
    }

}




























